<template>
  <div>
    <!-- 使用腾讯云与el-upload上传
      el-upload
         action:上传的接口地址（默认组件帮我们调用接口）
         name:指定上传接口对应的参数名，默认叫file
         show-file-list:是否显示文件列表，默认：显示
         on-success:上传成功后的回调
         before-upload:上传前的回调函数
         http-request	覆盖默认的上传行为，可以自定义上传的实现
     -->
    <el-upload
      action="http://119.91.150.211:8080/heimamm/public/uploads"
      name="image"
      :show-file-list="false"
      :before-upload="beforeUpload"
      :on-success="onSuccess"
    >

      <div v-if="!avatar">上传头像</div>
      <img v-else :src="avatar" alt="">
    </el-upload>

  </div>
</template>
<script>
export default {
  data() {
    return {
      avatar: ''
    }
  },
  methods: {
    beforeUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG,PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    onSuccess(res) {
      this.avatar = 'http://119.91.150.211:8080/heimamm/public/' + res.data.file_path
      console.log(res)
    }
  }
}
</script>
<style>
</style>
